package com.gxuwz.library_consumer.controller;

import com.github.pagehelper.PageInfo;
import com.gxuwz.commonmodule.entity.Account;
import com.gxuwz.commonmodule.entity.Admin;
import com.gxuwz.commonmodule.entity.Book;
import com.gxuwz.commonmodule.entity.User;
import com.gxuwz.library_consumer.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {
    /**
     * 不同功能对应的视图文件名称,注意不能包含后缀名
     */
    private String view_main = "adminMain";//管理员主页
    private String view_list = "admin/admin_list";//管理员信息列表
    private String view_input = "admin/admin_add";//添加管理员信息
    private String view_edit = "admin/admin_edit";//编辑管理员信息
    private String view_info = "admin/admin_message";//管理员个人信息
    private String redirect_list = "redirect:/admin/findPage?pageIndex=1";

    @Autowired
    private AccountService accountService;

    /**
     * 跳转到注册页面
     * @return
     */
    @GetMapping(value = "/toRegister")
    public String toRegister(){
        return "register";
    }

    /**
     * 注册处理
     * @param username
     * @param password
     * @param role
     * @return
     */
    @GetMapping("/register")
    public String register(String username, String name, String password, String email,
                           String phone, String address, String balance, String role, HttpServletRequest request) {
        if (username == null){
            System.out.println("注册失败");
            return "register";
        }
        accountService.register(username,name,password,email,phone,address,balance,role);
        System.out.println("注册成功");
        return "login";


//        if (username == null){
//            System.out.println("注册失败");
//            return "register";
//        }
//        System.out.println("到这");
//        userService.register(username,password,role,phone,name,id,email,address,balance);
//        System.out.println("注册成功");
//        return "login";
    }



    /**
     * 跳转到登陆页面
     * @return
     */
    @GetMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    /**
     * 登陆处理
     * @param username
     * @param password
     * @param role
     * @param session
     * @return
     */
    @PostMapping("/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("role") String role, HttpSession session)
    {
        Account account=accountService.login(username,password,role);
        String result=null;
        if(account==null)
        {
            result="login";
        }
        else{
            switch (role){
                case "读者":
                    User user=new User();
                    user.setUsername(account.getUsername());
                    user.setPassword(account.getPassword());
                    user.setRole(account.getRole());
                    user.setName(account.getName());
                    user.setEmail(account.getEmail());
                    user.setPhone(account.getPhone());
                    user.setAddress(account.getAddress());
                    user.setBalance(account.getBalance());
                    session.setAttribute("account",user);
                    result="redirect:/user/userMain";
                    System.out.println("读者登陆成功");
                    break;
                case "管理员":
                    Admin admin=new Admin();
                    admin.setUsername(account.getUsername());
                    admin.setPassword(account.getPassword());
                    admin.setRole(account.getRole());
                    session.setAttribute("account",admin);
                    result="redirect:/account/adminMain";
                    System.out.println("管理员登陆成功");
                    break;
            }
        }
        return result;
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "login";
    }


    /**
     * 管理员首页
     * @return
     */
    @RequestMapping("/adminMain")
    public String toUserMain(){
        return view_main;
    }

    /**
     * 管理员个人信息
     * @param username
     * @param model
     * @return
     */
    @GetMapping("/{username}")
    public String getAdmin(@PathVariable("username") String username, Model model) {
        Admin admin = accountService.findInfo(username);
        model.addAttribute("admin", admin);
        return view_info;
    }

    /**
     * 管理员信息列表
     * @return
     */
    @GetMapping("/list")
    public ModelAndView listAdmin(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(view_list);
        List<Admin> adminList = accountService.list(null);
        modelAndView.addObject("adminList",adminList);
        return modelAndView;
    }

    /**
     * 跳入增加管理员视图
     * @return
     */
    @RequestMapping("/input")
    public String input(){
        return view_input;
    }

    /**
     * 增加管理员信息
     * @param admin
     * @return
     */
    @PostMapping("/add")
    public String add(Admin admin){
        accountService.insert(admin);
        System.out.println("添加管理员成功");
        return redirect_list;
    }

    /**
     * 通过username删除管理员信息
     * @param username
     * @return
     */
    @GetMapping("/delete")
    public String delete(String username){
        accountService.deleteAdmin(username);
        return redirect_list;
    }

    /**
     * 跳转到编辑管理员视图
     * @param username
     * @param model
     * @return
     */
    @GetMapping("/get")
    public String get(String username, Model model){
        Admin admin = accountService.findById(username);
        model.addAttribute("m",admin);
        return view_edit;
    }

    /**
     * 编辑管理员信息
     * @param admin
     * @return
     */
    @PostMapping("/update")
    public String update(Admin admin){
        accountService.updateAdmin(admin);
        return redirect_list;
    }

    /**
     * 分页查询（不带条件）
     * @param model
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/findPage")
    public String findAll(Model model, @RequestParam(value = "pageIndex",defaultValue = "1")int pageIndex,
                          @RequestParam(value = "pageSize",defaultValue = "4",required = false)int pageSize){
        PageInfo page = accountService.findPage(pageIndex,pageSize);
        model.addAttribute("path","/admin/findPage?pageIndex=");
        model.addAttribute("page",page);
        return view_list;
    }
}
